﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mscook.core.services;
using mscook.core;
using mscook.core.domain;
using mscook.mvc.core;
using mscook.mvc.Areas.weblogin.Models;
using System.Linq.Expressions;
namespace mscook.mvc.Areas.weblogin.Controllers
{
    public class ChefController : BaseController
    {
        private IChefService iChefService;
        private IImageSettingFactory iImageSettingFactory;
        private ILoginUserService iLoginUserService;
        private IHotelService iHotelService;
        private readonly string SectionName = "Chef";
        public ChefController(IChefService iChefService, IImageSettingFactory iImageSettingFactory, IHotelService iHotelService, ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iHotelService = iHotelService;
            this.iChefService = iChefService;
            this.iImageSettingFactory = iImageSettingFactory;
            this.iLoginUserService = iLoginUserService;
        }
        public ActionResult Index(string nowPageNum,string position="",string chefName="", string per = "")
        {
            Expression<Func<Chef, bool>> select_expression = o => (string.IsNullOrEmpty(chefName) || o.Name.Contains(chefName))
              && (string.IsNullOrEmpty(position) || o.Position.Contains(position));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<Chef> chefList = iChefService.SelectPageListByExpressions(pageHelper.Start, pageHelper.PerValue, out total, select_expression);

            InitNavBar();
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            ViewData["chefName"] = chefName;
            ViewData["position"] = position;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;

            return View(chefList);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Add()
        {
            InitNavBar();
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Add(Chef chef, string ChefTypeId, string HotelId,bool IsTop=false)
        {
            chef.Hotel = iHotelService.Get(HotelId);


            if (ModelState.IsValid)
            {
                chef.EntityIndex = IsTop ? 1 : 0;
                SaveResourceType(chef, ChefTypeId);
                SaveUploadImage(chef);
                iChefService.SaveOrUpdate(chef);
                return RedirectToIndex();
            }
            else
            {
                IList<ModelErrorCollection> message = ModelState.Select(o => o.Value.Errors).ToList();
            }

            return View();

        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {
            Chef chef = iChefService.Get(id);
            if (chef != null)
            {
                ChefModel chefModel = new ChefModel() { Chef = chef };
                chefModel.ResourceTypes = GetResourceType(chef.Id);
                InitNavBar();
                return View(chefModel);
            }
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(string Id, string HotelId, string ChefTypeId, ChefModel chefModel,bool IsTop)
        {

            chefModel.Chef = iChefService.Get(Id);
            Hotel hotel = iHotelService.Get(HotelId);
            if (ModelState.IsValid)
            {
                chefModel.Chef.EntityIndex = IsTop ? 1 : 0;
                SaveResourceType(chefModel.Chef, ChefTypeId);
                SaveUploadImage(chefModel.Chef);
                UpdateModel(chefModel);
                chefModel.Chef.Hotel = hotel;

                iChefService.SaveOrUpdate(chefModel.Chef);
                return RedirectToIndex();
            }


            return View(chefModel);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(string id)
        {
            iChefService.Delete(id);
            return RedirectToIndex();
        }


        public ActionResult ChefModal(string nameInput, string idInput,string chefName, string linkBtn = "", string num = "")
        {
            
            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(num, "3");
            long total = 0;
            IList<Chef> cheflList = iChefService.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total,chefName);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);
            ViewData["nameInput"] = nameInput;
            ViewData["idInput"] = idInput;
            ViewData["linkBtn"] = linkBtn;

            return View(cheflList);
        }

        public bool SaveUploadImage(Chef Chef)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files.Get(0);
                if (file != null && file.InputStream != null && file.ContentLength != 0)
                {
                    Chef.ImageName = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
                }
                return true;
            }
            return false;
        }


    }
}
